<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\CashCode;
use think\Db;
use think\Request;
/**
 * 关键词搜索
 */
class BlogSearch extends Api
{

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页  http://fadmin.uni.com/api/blog_search
     * 
     */
    public function index()
    {	
		$this->success('请求成功');
    }
	
	
	//前台搜索框动态匹配
	//$.post("http://fadmin.uni.com/api/blog_search/search_ajax.html",{keyword:'test'},function(res){console.log(res.status)  ; 
	//if(res.status == 'success'){    $.each(res.data,function(key,obj){ console.log(obj.id_cms)  }); } });
	
/* 	
	$.post("https://api.uniwigs.com/api/blog_search/search_ajax.html",
	{keyword:'test' },function(res){

		console.log(res.status)  ; 
		
		if(res.status == 'success'){
			if(res.data.length  == 0){
				
				console.log('no result');
				
			}else{
				
					$.each(res.data,function(key,obj){ 
			
					console.log(obj.id_cms  +  obj.name  + obj.description  + obj.url)  
			
				});  
				
			}
		
		
		}
		 
		
		
	});
	$.post("https://api.uniwigs.com/api/blog_search/search_ajax.html",
	{keyword:'test' , page:1, item: 2},function(res){

		console.log(res.status)  ; 
		
		if(res.status == 'success'){
			
			if(res.data.length  == 0 ){
				
				console.log('no result');
				
			}else{
				
					$.each(res.data,function(key,obj){ 
			
					console.log(obj.id_cms  +  obj.name  + obj.description  + obj.url)  
			
				});  
				
			} 
		
		}
		 
		
		
	}); */
  
	public function search_ajax(){
		header('Content-Type:application/json; charset=utf-8');
		if($this->request->isPost()  OR  $this->request->isGet() ){
			//Request::instance()->filter('htmlspecialchars');// 设置输入过滤 单个
			Request::instance()->filter(['strip_tags','htmlspecialchars','trim']);//多个strip_tags php html 标签及内容过滤htmlspecialchars 单双引号 特殊字符过滤
	
			$keyword = $this->request->post("keyword");//这里的输入个词汇过滤 在全局的config.php 文件中配置
			if($keyword == ''){
				
				die('keyword is empty');
			}
			
			$page = $this->request->post("page")? $this->request->post("page"): 1;
			
			$item = $this->request->post("item")? $this->request->post("item"): 5;

			$result['status'] = 'success';
			$result['data'] = $this->q_cms($keyword,$item,$page);
		
			die( json_encode($result));
			
	
		}
		
		
	}
	
	private function  q_cms($keyword,$item,$page = 1 ){
        
        
        if(true){
            
           return  $this-> q_cms_new($keyword,$item,$page = 1 );
            
        }else{
            
          return   $this-> q_cms_old($keyword,$item,$page = 1 );
            
        }
        
        
    }
	
	

	//依据 博客名称  博客tag 博客描述   博客内容 进行匹配
    private function  q_cms_new($keyword,$item,$page = 1 ){
        
        $res = [];
        $title_res =  $this->q_cms_title($keyword,$item,$page) ; 
       
		if($title_res != '[]'){
			
			$res['title'] =  $title_res; 
            $rres[] = $res['title'];
            if(count($title_res)  < $item){

                $filter = '';
                foreach($res['title'] as $a){
                    
                     $filter .= $a['id_cms'] .',';
                     
                }
                
                $filter = rtrim($filter ,',');
                $content_res  =  $this->q_cms_content($keyword,( $item  -  count($title_res) ),$page ,$filter) ;
                if(  $content_res  != '[]'){
                    
                   $res['content'] =  $content_res; 
                   
                   foreach($res['content']  as  $a){
                       
                       $rres[0][] = $a;
                   }
                   
                }
            }
            $res = $rres[0] ;           
			
		}else{

            if($this->q_cms_content($keyword,$item,$page) != '[]'){

                 $res = $this->q_cms_content($keyword,$item,$page);
                
            }
   
        }
        return $res;
     
      
        
    }
    
    
    
    //旧版 blog查询
	private function q_cms_old($keyword,$item,$page){
		
		$res = [];
		if($this->q_cms_title($keyword,$item,$page) != '[]'){
			
			$res['title'] =  $this->q_cms_title($keyword,$item,$page) ; 
			return  $this->q_cms_title($keyword,$item,$page) ; 
			
		}
		
		if($this->q_cms_description($keyword,$item,$page) != '[]'){
			
			//$res['description'] =  $this->q_cms_description($keyword,$item,$page) ;
			return  $this->q_cms_description($keyword,$item,$page) ; 
			
		}
		
		if($this->q_cms_tag($keyword,$item,$page) != '[]'){
			
			//$res['tag'] =$this->q_cms_tag($keyword,$item,$page);
			return  $this->q_cms_tag($keyword,$item,$page) ; 
			
		}
		
		if($this->q_cms_content($keyword,$item,$page) != '[]'){
			
			$res['content'] =$this->q_cms_content($keyword,$item,$page);
			return  $this->q_cms_content($keyword,$item,$page) ; 
			
		}
		
    
        return  $res ;
	}
	
	
	//包含匹配 cms title 
	private function q_cms_title($keyword,$item = 5,$page = 1){
	
		$sql ="select    'title' as type,a.id_cms,meta_title as  `name`,meta_description as `description`, meta_keywords as `tag` ,
				CONCAT('/blog/',link_rewrite,'.html') as url ,b.date_add
				from  ps_cms_lang a
				LEFT JOIN  ps_cms  b  on a.id_cms =  b.id_cms   
				LEFT JOIN  ps_cms_category  d  on  b.id_cms_category  = d.id_cms_category
				where      a.meta_title  like '%".$keyword."%'     and a.id_lang =1 and a.id_shop =1 
				and b.active = 1  and  (d.id_cms_category = 9  or  d.id_parent = 9) 
                    order by  a.id_cms desc 
				limit ".($page-1)*$item  ."," .$item ;
		$res = Db::query($sql);
		
		return  $res?$res:'[]';

	
	}
	
	//包含匹配 cms description 
	private function q_cms_description($keyword,$item = 5,$page = 1){

		$sql ="select a.id_cms,meta_title as  `name`,meta_description as `description`, meta_keywords as `tag` ,
				CONCAT('/blog/',link_rewrite,'.html') as url  ,b.date_add
				from  ps_cms_lang a
				LEFT JOIN  ps_cms  b  on a.id_cms =  b.id_cms   
				LEFT JOIN  ps_cms_category  d  on  b.id_cms_category  = d.id_cms_category
				where a.meta_description  like '%".$keyword."%'  and a.id_lang =1 and a.id_shop =1 
				and b.active = 1  and  (d.id_cms_category = 9  or  d.id_parent = 9)
                    order by  a.id_cms desc 
				limit ".($page-1)*$item  ."," .$item;
		
		$res = Db::query($sql);
		
		return  $res?$res:'[]';
		
	
	}
	
	
	
	//包含匹配 cms title 
	private function q_cms_tag($keyword,$item = 5,$page = 1){
		
		$sql ="select a.id_cms,meta_title as  `name`,meta_description as `description`, meta_keywords as `tag` ,
				CONCAT('/blog/',link_rewrite,'.html') as url ,b.date_add
				from  ps_cms_lang a
				LEFT JOIN  ps_cms  b  on a.id_cms =  b.id_cms   
				LEFT JOIN  ps_cms_category  d  on  b.id_cms_category  = d.id_cms_category
				where a.meta_keywords  like '%".$keyword."%'  and a.id_lang =1 and a.id_shop =1 
				and b.active = 1  and  (d.id_cms_category = 9  or  d.id_parent = 9)
                    order by  a.id_cms desc 
				limit ".($page-1)*$item  ."," .$item;
		
		$res = Db::query($sql);

		return  $res?$res:'[]';	
	}
	
	
		
	//包含匹配 cms content 
	private function q_cms_content($keyword,$item = 5,$page = 1 , $filter=false){
        
        
        if($filter ){
            
            $filter =  ' and a.id_cms  not in (' . $filter . ') ';
            
        }else{
            
            $filter = '';
        }
		
		$sql ="select  'content' as type,  a.id_cms,meta_title as  `name`,meta_description as `description`, meta_keywords as `tag` ,
				CONCAT('/blog/',link_rewrite,'.html') as url ,b.date_add
				from  ps_cms_lang a
				LEFT JOIN  ps_cms  b  on a.id_cms =  b.id_cms   
				LEFT JOIN  ps_cms_category  d  on  b.id_cms_category  = d.id_cms_category
				where a.content  like '%".$keyword."%'  and a.id_lang =1 and a.id_shop =1 
				and b.active = 1  and  (d.id_cms_category = 9  or  d.id_parent = 9)
                
                ". $filter."
                    order by  a.id_cms desc 
				limit ".($page-1)*$item  ."," .$item;
		$res = Db::query($sql);

		return  $res?$res:'[]';
	
	}
	

}

